package cn.iocoder.yudao.module.exam.controller.admin.aimarkingtask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.aimarkingtask.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.aimarkingtask.AiMarkingTaskDO;
import cn.iocoder.yudao.module.exam.service.aimarkingtask.AiMarkingTaskService;

@Tag(name = "管理后台 - AI批阅任务")
@RestController
@RequestMapping("/exam/ai-marking-task")
@Validated
public class AiMarkingTaskController {

    @Resource
    private AiMarkingTaskService aiMarkingTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建AI批阅任务")
    @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:create')")
    public CommonResult<Long> createAiMarkingTask(@Valid @RequestBody AiMarkingTaskSaveReqVO createReqVO) {
        return success(aiMarkingTaskService.createAiMarkingTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI批阅任务")
    @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:update')")
    public CommonResult<Boolean> updateAiMarkingTask(@Valid @RequestBody AiMarkingTaskSaveReqVO updateReqVO) {
        aiMarkingTaskService.updateAiMarkingTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI批阅任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:delete')")
    public CommonResult<Boolean> deleteAiMarkingTask(@RequestParam("id") Long id) {
        aiMarkingTaskService.deleteAiMarkingTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI批阅任务")
                @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:delete')")
    public CommonResult<Boolean> deleteAiMarkingTaskList(@RequestParam("ids") List<Long> ids) {
        aiMarkingTaskService.deleteAiMarkingTaskListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI批阅任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:query')")
    public CommonResult<AiMarkingTaskRespVO> getAiMarkingTask(@RequestParam("id") Long id) {
        AiMarkingTaskDO aiMarkingTask = aiMarkingTaskService.getAiMarkingTask(id);
        return success(BeanUtils.toBean(aiMarkingTask, AiMarkingTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI批阅任务分页")
    @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:query')")
    public CommonResult<PageResult<AiMarkingTaskRespVO>> getAiMarkingTaskPage(@Valid AiMarkingTaskPageReqVO pageReqVO) {
        PageResult<AiMarkingTaskDO> pageResult = aiMarkingTaskService.getAiMarkingTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiMarkingTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI批阅任务 Excel")
    @PreAuthorize("@ss.hasPermission('exam:ai-marking-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAiMarkingTaskExcel(@Valid AiMarkingTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiMarkingTaskDO> list = aiMarkingTaskService.getAiMarkingTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI批阅任务.xls", "数据", AiMarkingTaskRespVO.class,
                        BeanUtils.toBean(list, AiMarkingTaskRespVO.class));
    }

}